package com.ly.mp.dal.comm.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.ly.mp.dal.comm.helper.SecurityHelper;


@Component
@Lazy(false)
@ConfigurationProperties(prefix = "write.mp.jdbc", ignoreUnknownFields = true)
public class DataSourceInfo {
    private static final Logger logger = LoggerFactory.getLogger(DataSourceInfo.class);
    private String name;
    private String url;
    private String username;
    private String password;
    private String transactionPolicy;
    private boolean upperCaseColumn = true;
    private String origName;
    public static final String DB_WRITE_KEY_SUFFIX = "_writeDBKey";
    public static final String DB_READ_KEY_SUFFIX = "_readDBKey";

    public String getName() {
        return this.name;
    }

    public String getUrl() {
        return this.url;
    }

    public String getUsername() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setUrl(String url) {
        this.url = this.decryptProperty("jdbc.url", url);
    }

    public void setUsername(String username) {
        this.username = this.decryptProperty("jdbc.username", username);
    }

    public void setPassword(String password) {
        this.password = this.decryptProperty("jdbc.password", password);
    }

    public String getTransactionPolicy() {
        return this.transactionPolicy;
    }

    public void setTransactionPolicy(String transactionPolicy) {
        this.transactionPolicy = transactionPolicy;
    }

    public boolean isUpperCaseColumn() {
        return this.upperCaseColumn;
    }

    public void setUpperCaseColumn(boolean upperCaseColumn) {
        this.upperCaseColumn = upperCaseColumn;
    }

    protected String decryptProperty(String key, String val) {
        try {
            if (val.length() >= 8) {
                return SecurityHelper.DBDecrypt(val);
            }
        } catch (Exception var4) {
            logger.info("数据库连接配置未加密 或 加密字符串错误！key: {}, value : {}" ,key,val);
        }

        return val;
    }

    public String getOrigName() {
        return this.origName;
    }

    public void setOrigName(String origName) {
        this.origName = origName;
    }
    
    public static void main(String[] args) {
        String dbEncrypt = SecurityHelper.DBEncrypt("jdbc:oracle:thin:@172.19.24.46:1521/DBSAP");
        System.out.println(dbEncrypt);
        System.out.println(SecurityHelper.DBDecrypt(dbEncrypt));
    }
}